package com.bluesky.drcp.presentation.web.webservice;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.rpc.holders.ByteArrayHolder;
import javax.xml.rpc.holders.StringHolder;

import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

import com.bluesky.drcp.business.Function;
import com.bluesky.drcp.business.FunctionModule;
import com.bluesky.drcp.business.Module;
import com.bluesky.drcp.business.Plugin;
import com.bluesky.drcp.business.Role;
import com.bluesky.drcp.business.User;
import com.bluesky.drcp.business.logic.DrcpFacade;

public class DrcpService extends ServletEndpointSupport {
	private DrcpFacade drcpFacade;

	private Logger logger;

	protected void onInit() {
		logger = Logger.getLogger(this.getClass().toString());
		this.drcpFacade = (DrcpFacade) getWebApplicationContext().getBean("drcp");
	}

	public String ClientConfig_GetContent(String host, String module) {

		return drcpFacade.getClientConfigService().getContent(host, module);
	}

	public void ClientConfig_SetContent(String host, String module,
			String content) {
		drcpFacade.getClientConfigService().setContent(host, module, content);

	}

	public int Test(String workOrder) {
		return workOrder.length();
	}

	public void Log_Add(String userId, String computerName, String source,
			int type, String category, String action, String comment) {

		drcpFacade.getLogService().addLog(userId, computerName, source, type,
				category, action, comment);
	}

	public Function Function_Get(String id) {
		return drcpFacade.getFunctionService().get(id);
	}

	public Function Function_GetRoot() {

		return drcpFacade.getFunctionService().getRoot();
	}

	public Function Function_GetParent(String id) {
		return drcpFacade.getFunctionService().getParent(id);
	}

	public Function[] Function_GetChildren(String id) {

		List list = drcpFacade.getFunctionService().getChildren(id);
		Function[] rslt = new Function[list.size()];
		list.toArray(rslt);
		return rslt;
	}

	public String Function_GetPath(String id) {
		return drcpFacade.getFunctionService().getPath(id);
	}

	public Function[] Function_GetAll() {
		List funcList = drcpFacade.getFunctionService().getAll();
		if (funcList != null)
			return (Function[]) funcList.toArray(new Function[] {});
		else
			return new Function[] {};
	}

	public String Function_Save(Function function) {
		return drcpFacade.getFunctionService().save(function);
	}

	public void Function_Update(Function function) {
		drcpFacade.getFunctionService().update(function);
	}

	public void Function_Delete(String id) {
		drcpFacade.getFunctionService().delete(id);
	}

	public boolean FunctionAuthorization_IsUserAuthorized(String userId,
			String functionId) {
		return drcpFacade.getFunctionAuthorizationService().isUserAuthorized(
				userId, functionId);
	}

	public boolean FunctionAuthorization_IsRoleAuthorized(String roleId,
			String functionId) {
		return drcpFacade.getFunctionAuthorizationService().isRoleAuthorized(
				roleId, functionId);
	}

	public boolean FunctionAuthorization_IsUserAuthorizedByPath(String userId,
			String functionPath) {
		return drcpFacade.getFunctionAuthorizationService()
				.isUserAuthorizedByPath(userId, functionPath);
	}

	public Function[] FunctionAuthorization_GetAuthorizedFunctions(String roleId) {
		List al = drcpFacade.getFunctionAuthorizationService()
				.getAuthorizedFunctions(roleId);
		Function[] rslt = (Function[]) al.toArray(new Function[] {});
		if (rslt == null)
			rslt = new Function[] {};

		return rslt;
	}

	public void FunctionAuthorization_SetAuthorizedFunctions(String roleId,
			String[] functionIds) {
		List alFunctionIds = new ArrayList();
		for (int i = 0; i < functionIds.length; i++)
			alFunctionIds.add(functionIds[i]);

		drcpFacade.getFunctionAuthorizationService().setAuthorizedFunctions(
				roleId, alFunctionIds);
	}

	public Module Module_Get(String id) {
		return drcpFacade.getModuleService().get(id);
	}

	public Module Module_GetByFunctionId(String functionId) {
		return drcpFacade.getModuleService().getByFunctionId(functionId);
	}

	public Module[] Module_GetAll() {
		List moduleList = drcpFacade.getModuleService().getAll();
		if (moduleList != null)
			return (Module[]) moduleList.toArray(new Module[] {});
		else
			return new Module[] {};
	}

	public void Module_Save(Module module) {
		drcpFacade.getModuleService().save(module);
	}

	public void Module_Update(Module module) {
		drcpFacade.getModuleService().update(module);
	}

	public void Module_Delete(String moduleId) {
		drcpFacade.getModuleService().delete(moduleId);
	}

	public boolean FunctionModule_IsModule(String functionId) {
		return drcpFacade.getFunctionModuleService().isModule(functionId);
	}

	public Module FunctionModule_GetModuleByFunctionId(String functionId) {
		return drcpFacade.getFunctionModuleService().getModuleByFunctionId(
				functionId);
	}

	public void FunctionModule_SaveOrUpdate(FunctionModule functionModule) {
		drcpFacade.getFunctionModuleService().saveOrUpdate(functionModule);
	}

	public void FunctionModule_Delete(String functionId) {
		drcpFacade.getFunctionModuleService().delete(functionId);
	}

	public boolean Login(String userId, String password) {
		return drcpFacade.getUserService().login(userId, password);
	}

	public void Plugin_GetById(String id, StringHolder name,
			StringHolder version, StringHolder desc, ByteArrayHolder data)
			throws RemoteException {
		Plugin o = drcpFacade.getPluginService().get(id);

		name.value = o.getName();
		desc.value = o.getDesc();
		data.value = o.getData();

	}

	public Plugin Plugin_Get(String id) {
		return drcpFacade.getPluginService().get(id);
	}

	public Plugin[] Plugin_GetAll(boolean fetchData) {
		List plugins = drcpFacade.getPluginService().getAll();
		Plugin[] rslt;
		if (plugins != null)
			rslt = (Plugin[]) plugins.toArray(new Plugin[] {});
		else
			rslt = new Plugin[] {};

		if (!fetchData) {
			for (int i = 0; i < rslt.length; i++)
				rslt[i].setData(new byte[] {});
		}

		return rslt;
	}

	public void Plugin_GetByName(String name, StringHolder version,
			StringHolder desc, ByteArrayHolder data) throws RemoteException {
		Plugin o = drcpFacade.getPluginService().getByName(name);

		desc.value = o.getDesc();
		data.value = o.getData();

	}

	public String Plugin_GetNameById(String id) {
		return drcpFacade.getPluginService().getNameById(id);
	}

	public String Plugin_GetVersionByName(String name) throws RemoteException {
		Plugin o = drcpFacade.getPluginService().getByName(name);

		return o.getVersion();
	}

	public byte[] Plugin_GetDataByName(String name) throws RemoteException {
		Plugin o = drcpFacade.getPluginService().getByName(name);

		return o.getData();
	}

	public void Plugin_InsertOrUpdateByName(String name, String version,
			String desc, byte[] data) throws RemoteException {
		Plugin o = new Plugin();
		o.setName(name);
		o.setVersion(version);
		o.setDesc(desc);
		o.setData(data);

		drcpFacade.getPluginService().saveOrUpdateByName(o);

	}

	public String GlobalVariable_GetValue(String key) {
		return drcpFacade.getGlobalVariableService().getValue(key);
	}

	public void GlobalVariable_SetValue(String key, String value) {
		drcpFacade.getGlobalVariableService().set(key, value);
	}

	/**
	 * confirm password from local table
	 * 
	 * @seealso login through other way,such as webservice
	 * @param userId
	 * @param password
	 * @return
	 */
	public boolean User_Login(String userId, String password) {
		return drcpFacade.getUserService().login(userId, password);
	}

	public User User_Get(String id) {
		return drcpFacade.getUserService().get(id);
	}

	public User[] User_GetAll() {
		User[] rslt = (User[]) drcpFacade.getUserService().getAll().toArray(
				new User[] {});

		if (rslt == null)
			rslt = new User[] {};

		return rslt;
	}

	public void User_Save(User user) {
		drcpFacade.getUserService().save(user);
	}

	public void User_Update(User user) {
		drcpFacade.getUserService().update(user);
	}

	public void User_Delete(String id) {
		drcpFacade.getUserService().delete(id);
	}

	public Role[] UserRole_GetRoleList(String userId) {

		Object[] toArray = drcpFacade.getUserRoleService().getRoleList(userId)
				.toArray(new Role[] {});

		Role[] rslt = (Role[]) toArray;
		if (rslt == null)
			rslt = new Role[] {};

		return rslt;
	}

	public void UserRole_SetRoleList(String userId, String[] roleIdList) {
		List al = new ArrayList();
		for (int i = 0; i < roleIdList.length; i++)
			al.add(roleIdList[i]);

		drcpFacade.getUserRoleService().setRoleList(userId, al);
	}

	public void Role_Delete(String id) {
		drcpFacade.getRoleService().delete(id);
	}

	public Role Role_Get(String id) {
		return drcpFacade.getRoleService().get(id);
	}

	public Role[] Role_GetAll() {
		Role[] rslt = (Role[]) drcpFacade.getRoleService().getAll().toArray(
				new Role[] {});

		if (rslt == null)
			rslt = new Role[] {};

		return rslt;
	}

	public String Role_Save(Role role) {
		return drcpFacade.getRoleService().save(role);
	}

	public void Role_Update(Role role) {
		drcpFacade.getRoleService().update(role);
	}
}